<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Basic Asm (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Basic Asm (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Basic Asm (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Using-Assembly-Language-with-C.html" rel="up" title="Using Assembly Language with C">
<link href="Extended-Asm.html" rel="next" title="Extended Asm">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>


</head>

<body lang="en_US">
<div class="subsection-level-extent" id="Basic-Asm">
<div class="nav-panel">
<p>
Next: <a href="Extended-Asm.html" accesskey="n" rel="next">Extended Asm - Assembler Instructions with C Expression Operands</a>, Up: <a href="Using-Assembly-Language-with-C.html" accesskey="u" rel="up">How to Use Inline Assembly Language in C Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Basic-Asm-_002d_002d_002d-Assembler-Instructions-Without-Operands"><span>6.47.1 Basic Asm &mdash; Assembler Instructions Without Operands<a class="copiable-link" href="#Basic-Asm-_002d_002d_002d-Assembler-Instructions-Without-Operands"> &para;</a></span></h4>
<a class="index-entry-id" id="index-basic-asm"></a>
<a class="index-entry-id" id="index-assembly-language-in-C_002c-basic"></a>

<p>A basic <code class="code">asm</code> statement has the following syntax:
</p>
<div class="example">
<pre class="example-preformatted">asm <var class="var">asm-qualifiers</var> ( <var class="var">AssemblerInstructions</var> )
</pre></div>

<p>For the C language, the <code class="code">asm</code> keyword is a GNU extension.
When writing C code that can be compiled with <samp class="option">-ansi</samp> and the
<samp class="option">-std</samp> options that select C dialects without GNU extensions, use
<code class="code">__asm__</code> instead of <code class="code">asm</code> (see <a class="pxref" href="Alternate-Keywords.html">Alternate Keywords</a>).  For
the C++ language, <code class="code">asm</code> is a standard keyword, but <code class="code">__asm__</code>
can be used for code compiled with <samp class="option">-fno-asm</samp>.
</p>
<h4 class="subsubheading" id="Qualifiers-1"><span>Qualifiers<a class="copiable-link" href="#Qualifiers-1"> &para;</a></span></h4>
<dl class="table">
<dt><code class="code">volatile</code></dt>
<dd><p>The optional <code class="code">volatile</code> qualifier has no effect. 
All basic <code class="code">asm</code> blocks are implicitly volatile.
</p>
</dd>
<dt><code class="code">inline</code></dt>
<dd><p>If you use the <code class="code">inline</code> qualifier, then for inlining purposes the size
of the <code class="code">asm</code> statement is taken as the smallest size possible (see <a class="pxref" href="Size-of-an-asm.html">Size of an <code class="code">asm</code></a>).
</p></dd>
</dl>

<h4 class="subsubheading" id="Parameters"><span>Parameters<a class="copiable-link" href="#Parameters"> &para;</a></span></h4>
<dl class="table">
<dt><var class="var">AssemblerInstructions</var></dt>
<dd><p>This is a literal string that specifies the assembler code. The string can 
contain any instructions recognized by the assembler, including directives. 
GCC does not parse the assembler instructions themselves and 
does not know what they mean or even whether they are valid assembler input. 
</p>
<p>You may place multiple assembler instructions together in a single <code class="code">asm</code> 
string, separated by the characters normally used in assembly code for the 
system. A combination that works in most places is a newline to break the 
line, plus a tab character (written as &lsquo;<samp class="samp">\n\t</samp>&rsquo;).
Some assemblers allow semicolons as a line separator. However, 
note that some assembler dialects use semicolons to start a comment. 
</p></dd>
</dl>

<h4 class="subsubheading" id="Remarks"><span>Remarks<a class="copiable-link" href="#Remarks"> &para;</a></span></h4>
<p>Using extended <code class="code">asm</code> (see <a class="pxref" href="Extended-Asm.html">Extended Asm - Assembler Instructions with C Expression Operands</a>) typically produces
smaller, safer, and more efficient code, and in most cases it is a
better solution than basic <code class="code">asm</code>.  However, there are two
situations where only basic <code class="code">asm</code> can be used:
</p>
<ul class="itemize mark-bullet">
<li>Extended <code class="code">asm</code> statements have to be inside a C
function, so to write inline assembly language at file scope (&ldquo;top-level&rdquo;),
outside of C functions, you must use basic <code class="code">asm</code>.
You can use this technique to emit assembler directives,
define assembly language macros that can be invoked elsewhere in the file,
or write entire functions in assembly language.
Basic <code class="code">asm</code> statements outside of functions may not use any
qualifiers.

</li><li>Functions declared
with the <code class="code">naked</code> attribute also require basic <code class="code">asm</code>
(see <a class="pxref" href="Function-Attributes.html">Declaring Attributes of Functions</a>).
</li></ul>

<p>Safely accessing C data and calling functions from basic <code class="code">asm</code> is more 
complex than it may appear. To access C data, it is better to use extended 
<code class="code">asm</code>.
</p>
<p>Do not expect a sequence of <code class="code">asm</code> statements to remain perfectly 
consecutive after compilation. If certain instructions need to remain 
consecutive in the output, put them in a single multi-instruction <code class="code">asm</code>
statement. Note that GCC&rsquo;s optimizers can move <code class="code">asm</code> statements 
relative to other code, including across jumps.
</p>
<p><code class="code">asm</code> statements may not perform jumps into other <code class="code">asm</code> statements. 
GCC does not know about these jumps, and therefore cannot take 
account of them when deciding how to optimize. Jumps from <code class="code">asm</code> to C 
labels are only supported in extended <code class="code">asm</code>.
</p>
<p>Under certain circumstances, GCC may duplicate (or remove duplicates of) your 
assembly code when optimizing. This can lead to unexpected duplicate 
symbol errors during compilation if your assembly code defines symbols or 
labels.
</p>
<p><strong class="strong">Warning:</strong> The C standards do not specify semantics for <code class="code">asm</code>,
making it a potential source of incompatibilities between compilers.  These
incompatibilities may not produce compiler warnings/errors.
</p>
<p>GCC does not parse basic <code class="code">asm</code>&rsquo;s <var class="var">AssemblerInstructions</var>, which
means there is no way to communicate to the compiler what is happening
inside them.  GCC has no visibility of symbols in the <code class="code">asm</code> and may
discard them as unreferenced.  It also does not know about side effects of
the assembler code, such as modifications to memory or registers.  Unlike
some compilers, GCC assumes that no changes to general purpose registers
occur.  This assumption may change in a future release.
</p>
<p>To avoid complications from future changes to the semantics and the
compatibility issues between compilers, consider replacing basic <code class="code">asm</code>
with extended <code class="code">asm</code>.  See
<a class="uref" href="https://gcc.gnu.org/wiki/ConvertBasicAsmToExtended">How to convert
from basic asm to extended asm</a> for information about how to perform this
conversion.
</p>
<p>The compiler copies the assembler instructions in a basic <code class="code">asm</code> 
verbatim to the assembly language output file, without 
processing dialects or any of the &lsquo;<samp class="samp">%</samp>&rsquo; operators that are available with
extended <code class="code">asm</code>. This results in minor differences between basic 
<code class="code">asm</code> strings and extended <code class="code">asm</code> templates. For example, to refer to 
registers you might use &lsquo;<samp class="samp">%eax</samp>&rsquo; in basic <code class="code">asm</code> and
&lsquo;<samp class="samp">%%eax</samp>&rsquo; in extended <code class="code">asm</code>.
</p>
<p>On targets such as x86 that support multiple assembler dialects,
all basic <code class="code">asm</code> blocks use the assembler dialect specified by the 
<samp class="option">-masm</samp> command-line option (see <a class="pxref" href="x86-Options.html">x86 Options</a>).  
Basic <code class="code">asm</code> provides no
mechanism to provide different assembler strings for different dialects.
</p>
<p>For basic <code class="code">asm</code> with non-empty assembler string GCC assumes
the assembler block does not change any general purpose registers,
but it may read or write any globally accessible variable.
</p>
<p>Here is an example of basic <code class="code">asm</code> for i386:
</p>
<div class="example">
<pre class="example-preformatted">/* Note that this code will not compile with -masm=intel */
#define DebugBreak() asm(&quot;int $3&quot;)
</pre></div>

</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Extended-Asm.html">Extended Asm - Assembler Instructions with C Expression Operands</a>, Up: <a href="Using-Assembly-Language-with-C.html">How to Use Inline Assembly Language in C Code</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
